from pyvision import Report, DataSet
import pandas as pd
import altair as alt
from vega_datasets import data


# 切片器同时关联 altair 图表与表格

source: pd.DataFrame = data.wheat()

# 以下是 altair 官网例子
chart = alt.Chart(source).mark_bar().encode(
    x='year:O',
    y="wheat:Q",
    # The highlight will be set on the result of a conditional statement
    color=alt.condition(
        alt.datum.year == 1810,  # If the year is 1810 this test returns True,
        alt.value('orange'),     # which sets the bar orange.
        # And if it's not true it sets the bar steelblue.
        alt.value('steelblue')
    )
).properties(width=600)

# 现在用 pyvision 输出
ds = DataSet(source)

(
    Report()
    .add_md('# 切片器同时关联 altair 图表与表格')
    .add_alt_chart(chart, ds)
    .add_row(ds.new_slicer('year'))
    .add_table(ds)
    .to_html('outputs/case4.html')
)
